﻿using System;
using OTE.Tasks.Commands.Test;
using SharpArch.Domain.Commands;
using SharpArch.NHibernate.Contracts.Repositories;

namespace OTE.Tasks.CommandHandlers.tmpTest
{
    public class TestMetaDataAddedHandler : ICommandHandler<TestMetaDataAddedCommand>
    {
        private readonly INHibernateRepository<Domain.Test> _testRepository;

        public TestMetaDataAddedHandler(INHibernateRepository<Domain.Test> testRepository)
        {
            _testRepository = testRepository;
        }

        public ICommandResult Handle(TestMetaDataAddedCommand command)
        {
            var test = new Domain.Test()
            {
                Content = command.Content,
                QuestionId = command.QuestionId,
                QuestionTypeId = command.QuestionTypeId,
                UserId = command.UserId,
                CreatedBy = command.CreatedBy,
                CreatedDate = DateTime.Now,
                ModifiedDate = DateTime.Now,
                ModifiedBy = command.CreatedBy
            };

            var t = _testRepository.Save(test);

            var result = t == null
                ? new TestMetaDataAddedResult(false)
                : new TestMetaDataAddedResult(true) { Test = t };

            return result;
        }
    }
}